<template>
  <div>
    <el-form :model="userForm" ref="userForm" label-width="100px" size="small" :rules="rules">
      <el-collapse class="custom-collapse" v-model="activeName">
        <el-collapse-item title="基本信息" name="1">
          <el-form-item label="管理员姓名:" prop="realName">
            <el-input v-model="userForm.realName" placeholder="请输入管理员姓名" maxlength="30" clearable></el-input>
          </el-form-item>
          <el-form-item label="所属渠道:" prop="organizationId">
            <organization-cascader :flag="'id'" v-model="userForm.organizationId"></organization-cascader>
          </el-form-item>
          <el-form-item label="当前职务:" prop="roleId">
            <el-select placeholder="请选择当前职务" clearable filterable v-model="userForm.roleId" style="width:100%">
              <el-option v-for="item in roleList" :key="item.roleId" :value="item.roleId" :label="item.roleName"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="管理员性别:" prop="gender">
            <el-radio-group v-model="userForm.gender">
              <el-radio v-for="(value, key) in dicts['gender'].childMap" :key="key" :label="key">{{ value }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="手机号码:" prop="phone">
            <el-input v-model="userForm.phone" placeholder="请输入手机号码" maxlength="11" clearable></el-input>
          </el-form-item>
        </el-collapse-item>
        <el-collapse-item title="更多信息" name="2">

          <el-form-item label="备注:" prop="remark">
            <el-input v-model="userForm.remark" type="textarea" placeholder="请输入备注" maxlength="300" show-word-limit clearable></el-input>
          </el-form-item>
        </el-collapse-item>
      </el-collapse>
      <el-form-item style="margin-top: 10px" label-width="130px">
        <el-button type="primary" @click="submit">
          <fa-icon class="fa-check" />提交</el-button>
        <el-button plain @click="resetData">
          <fa-icon class="fa-refresh" />重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { common } from '@/utils/common'
import { CONSTANT_OBJ } from '@/utils/constant'
import OrganizationCascader from '@/components/organizationCascader'
import api from '../api'
import { mapState } from 'vuex'

export default {
  props: { editUserData: Object, roleList: Array },
  components: { OrganizationCascader },
  data () {
    return {
      activeName: ['1'],
      common,
      userForm: {
        userId: '',

        realName: '',
        idNbr: '',
        organizationId: '',
        roleId: '',
        gender: '',
        phone: '',
        imgBase64: '',
        wxAccount: '',
        email: '',
        birthday: '',
        origin: '',
        graduateSchool: '',
        graduateTime: '',
        degree: '',
        profession: '',
        remark: ''
      },
      rules: {
        realName: [{ required: true, message: '请输入管理员姓名', trigger: 'blur' }],
        organizationId: [{ required: true, message: '请选择所属渠道', trigger: 'change' }],
        roleId: [{ required: true, message: '请选择当前职务', trigger: 'change' }],
        phone: [{ required: true, message: '请输入管理员手机号码', trigger: 'blur' }]
      }
    }
  },
  mounted () {
  },
  methods: {
    /**
     * 提交代码
     */
    submit () {
      this.$refs['userForm'].validate((valid) => {
        if (valid) {
          if (this.editUserData && this.editUserData.userId) {
            this.editUser()
          } else {
            this.addUser()
          }
        }
      })
    },
    getIdCard (val) {
      let sex = ''
      if (val.length >= 17) {
        if (parseInt(val.substr(16, 1)) % 2 === 1) {
          sex = Object.keys(this.dicts['gender'].childMap).filter(ele => {
            if (this.dicts['gender'].childMap[ele] === '男') {
              return ele
            }
          })[0]
        } else {
          sex = Object.keys(this.dicts['gender'].childMap).filter(ele => {
            if (this.dicts['gender'].childMap[ele] === '女') {
              return ele
            }
          })[0]
        }
        this.userForm.gender = sex
      }
    },
    // 新增
    addUser () {
      api.postAddUserData(this.userForm).then(res => {
        const { responseCode, responseMsg } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.$message({
            message: `${responseMsg}`,
            type: 'success'
          })
          this.$emit('uppdateUser', 'addUser')
        }
      })
    },
    // 编辑
    editUser () {
      api.putUserData(this.userForm).then(res => {
        const { responseCode, responseMsg } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.$message({
            message: `${responseMsg}`,
            type: 'success'
          })
          this.$emit('uppdateUser')
        }
      })
    },
    resetData () {
      if (this.editUserData && this.editUserData.userId) {
        common.listAssign(this.userForm, this.editUserData)
      } else {
        common.resetData(this.userForm)
      }
    }
  },
  computed: {
    ...mapState({
      dicts: state => state.common.dicts
    })
  },
  watch: {
    editUserData: {
      deep: true,
      handler (newValue) {
        if (this.editUserData && this.editUserData.userId) {
          common.listAssign(this.userForm, this.editUserData)
        }
      }
    }
  }
}
</script>
